/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Home.java
 *
 * Created on May 8, 2010, 6:12:57 PM
 */
package org.DragonPokerClient.graphic;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
import org.Comunicator.Message;
import org.DragonPokerClient.Classes.Room;
import org.DragonPokerClient.Client;
import org.DragonPokerClient.graphic.Extends.SortButtonRenderer;
import org.DragonPokerClient.graphic.Extends.SortableTableModel;

/**
 *
 * @author alien
 */
public class Home extends javax.swing.JFrame {

    SortableTableModel dm;

    /** Creates new form Home */
    public Home() {

        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        RoomsTable = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        RoomsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(RoomsTable);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 355, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(156, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(37, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Message m = new Message();
        m.setAction("Rooms");
        Client.sendMessage(m);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Home().setVisible(true);
            }
        });
    }

    public void prepareTable(ArrayList<Room> rooms) {
        String[] headerStr = {"Name", "Date"};
        int[] columnWidth = {100, 150};

        dm = new SortableTableModel() {

            public Class getColumnClass(int col) {
                switch (col) {
                    case 0:
                        return String.class;
                    default:
                        return String.class;
                }
            }

            public boolean isCellEditable(int row, int col) {
                switch (col) {
                    case 1:
                        return false;
                    default:
                        return true;
                }
            }

            public void setValueAt(Object obj, int row, int col) {
                switch (col) {


                    default:
                        super.setValueAt(obj, row, col);
                        return;
                }
            }
        };
        
        Object[][] o = new Object[rooms.size()][2];
        for (int i = 0; i < rooms.size(); i++) {

            o[i] = new Object[]{rooms.get(i), rooms.get(i).getName()};
        }

        dm.setDataVector(o, headerStr);
        RoomsTable.setModel(dm);
        SortButtonRenderer renderer = new SortButtonRenderer();
        TableColumnModel model = RoomsTable.getColumnModel();
        int n = headerStr.length;
        for (int i = 0; i < n; i++) {
            model.getColumn(i).setHeaderRenderer(renderer);
            model.getColumn(i).setPreferredWidth(columnWidth[i]);
        }

        JTableHeader header = RoomsTable.getTableHeader();
        header.addMouseListener(new HeaderListener(header, renderer));

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable RoomsTable;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    class HeaderListener extends MouseAdapter {

        JTableHeader header;
        SortButtonRenderer renderer;

        HeaderListener(JTableHeader header, SortButtonRenderer renderer) {
            this.header = header;
            this.renderer = renderer;
        }

        public void mousePressed(MouseEvent e) {
            int col = header.columnAtPoint(e.getPoint());
            int sortCol = header.getTable().convertColumnIndexToModel(col);
            renderer.setPressedColumn(col);
            renderer.setSelectedColumn(col);
            header.repaint();

            if (header.getTable().isEditing()) {
                header.getTable().getCellEditor().stopCellEditing();
            }

            boolean isAscent;
            if (SortButtonRenderer.DOWN == renderer.getState(col)) {
                isAscent = true;
            } else {
                isAscent = false;
            }
            ((SortableTableModel) header.getTable().getModel()).sortByColumn(sortCol, isAscent);
        }

        public void mouseReleased(MouseEvent e) {
            int col = header.columnAtPoint(e.getPoint());
            renderer.setPressedColumn(-1);                // clear
            header.repaint();
        }
    }
}
